package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @PostMapping
    public Object save(Travel travel){
        travel.setAuthor(UserContext.getCurrentUser());
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }
    @PutMapping("{id}")
    public Object update(Travel travel){
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    @GetMapping("{id}")
    public Travel getById(@PathVariable Long id){
        return travelService.getById(id);
    }

    @GetMapping
    public PageInfo queryForList(TravelQueryObject qo){
        qo.setState(Travel.STATE_RELEASE);
        qo.setOrderby("t.releaseTime desc");
        return travelService.queryForList(qo);
    }
}
